<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Html5,WebWorker," />





  <link rel="alternate" href="/atom.xml" title="OBKoro1's Blog" type="application/atom+xml" />




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.2" />






<meta name="description" content="我第一次对网页全屏模式有概念，是那种网页播放视频的全屏播 放的那种。感觉很强，前几个星期有个需求也是关于全屏模式的，接触之后才知道全屏模式并不神秘，是个很容易掌握的技能…">
<meta name="keywords" content="Html5,WebWorker">
<meta property="og:type" content="article">
<meta property="og:title" content="网页全屏模式轻松掌握[局部元素全屏展示]">
<meta property="og:url" content="http://yoursite.com/2018/12/02/网页全屏模式轻松掌握-局部元素全屏展示/index.html">
<meta property="og:site_name" content="OBKoro1&#39;s Blog">
<meta property="og:description" content="我第一次对网页全屏模式有概念，是那种网页播放视频的全屏播 放的那种。感觉很强，前几个星期有个需求也是关于全屏模式的，接触之后才知道全屏模式并不神秘，是个很容易掌握的技能…">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://github.com/OBKoro1/articleImg_src/blob/master/weibo_img_move/005Y4rCogy1fxsk0gb2d4j30jg0czdh6.jpg?raw=true">
<meta property="og:image" content="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/167690a2a5a9ba2c?w=1455&h=802&f=png&s=60702?raw=true">
<meta property="og:image" content="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/167694f2253893bf?w=1891&h=194&f=png&s=30483?raw=true">
<meta property="og:updated_time" content="2019-07-20T06:55:39.067Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="网页全屏模式轻松掌握[局部元素全屏展示]">
<meta name="twitter:description" content="我第一次对网页全屏模式有概念，是那种网页播放视频的全屏播 放的那种。感觉很强，前几个星期有个需求也是关于全屏模式的，接触之后才知道全屏模式并不神秘，是个很容易掌握的技能…">
<meta name="twitter:image" content="https://github.com/OBKoro1/articleImg_src/blob/master/weibo_img_move/005Y4rCogy1fxsk0gb2d4j30jg0czdh6.jpg?raw=true">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.2',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/12/02/网页全屏模式轻松掌握-局部元素全屏展示/"/>





  <title>网页全屏模式轻松掌握[局部元素全屏展示] | OBKoro1's Blog</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?64d57d0b926bbd27431daa89d2e0a76b";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">OBKoro1's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-前端算法">
          <a href="/arithmetic/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-book"></i> <br />
            
            前端算法
          </a>
        </li>
      
        
        <li class="menu-item menu-item-博客最新更新">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            博客最新更新
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/12/02/网页全屏模式轻松掌握-局部元素全屏展示/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="OBKoro1">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/hand1.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="OBKoro1's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">网页全屏模式轻松掌握[局部元素全屏展示]</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-12-02T18:07:33+08:00">
                2018-12-02
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/js/" itemprop="url" rel="index">
                    <span itemprop="name">js</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2018/12/02/网页全屏模式轻松掌握-局部元素全屏展示/" class="leancloud_visitors" data-flag-title="网页全屏模式轻松掌握[局部元素全屏展示]">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  2,334 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  9分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p><img src="https://github.com/OBKoro1/articleImg_src/blob/master/weibo_img_move/005Y4rCogy1fxsk0gb2d4j30jg0czdh6.jpg?raw=true" alt=""></p>
<p>我第一次对网页全屏模式有概念，是那种网页播放视频的全屏播 放的那种。感觉很强，前几个星期有个需求也是关于全屏模式的，接触之后才知道全屏模式并不神秘，是个很容易掌握的技能…</p>
<a id="more"></a>
<blockquote>
<p><a href="http://obkoro1.com/" target="_blank" rel="noopener">博客</a>、<a href="http://obkoro1.com/web_accumulate/accumulate/" target="_blank" rel="noopener">前端积累文档</a>、<a href="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/1631b6f52f7e7015.jpeg?raw=true" target="_blank" rel="noopener">公众号</a>、<a href="https://github.com/OBKoro1" target="_blank" rel="noopener">GitHub</a></p>
</blockquote>
<h3 id="CodePen-Demo"><a href="#CodePen-Demo" class="headerlink" title="CodePen Demo"></a>CodePen Demo</h3><p>地址：<a href="https://codepen.io/OBKoro1/full/wQmOzG/" target="_blank" rel="noopener">演示</a>、<a href="https://codepen.io/OBKoro1/pen/wQmOzG" target="_blank" rel="noopener">code</a></p>
<p>进去看看，玩一下，本文将结合这个demo一起进行讲解。</p>
<p><img src="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/167690a2a5a9ba2c?w=1455&amp;h=802&amp;f=png&amp;s=60702?raw=true" alt=""></p>
<h3 id="全屏功能封装在一个类里面："><a href="#全屏功能封装在一个类里面：" class="headerlink" title="全屏功能封装在一个类里面："></a>全屏功能封装在一个类里面：</h3><p>我把全屏模式封装在一个类里面，在代码中有详细的注释，如果有需要的话，直接把类拿出来，根据栗子和注释使用即可。</p>
<p>代码在<a href="https://codepen.io/OBKoro1/pen/wQmOzG" target="_blank" rel="noopener">codepen</a>的demo里。</p>
<h3 id="何谓全屏？"><a href="#何谓全屏？" class="headerlink" title="何谓全屏？"></a>何谓全屏？</h3><p><strong>MDN介绍</strong>：</p>
<p>使用提供的API，让一个元素与其子元素，可以占据整个屏幕，并在此期间，从屏幕上隐藏所有的浏览器用户界面以及其他应用。</p>
<p><strong>chrome下的全屏表现</strong>：</p>
<ol>
<li><p><strong>全屏会隐藏标签栏,书签栏</strong></p>
</li>
<li><p>如果网页一开始不是全部撑开的形式，全屏下，也会<strong>将要全屏的元素充满整个屏幕</strong></p>
</li>
<li><p><strong>可以多层全屏</strong>，如栗子中一样，可以先左边全屏，然后红色全屏。</p>
<p> 在这种情况下退出全屏，只会退出红色全屏，退回到左边全屏的形式，所以页面依然是全屏模式。</p>
</li>
<li><p>进入全屏时，有一个<strong>默认的提示</strong>：’按esc即可退出全屏模式’，如下图显示：</p>
<p> <img src="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/167694f2253893bf?w=1891&amp;h=194&amp;f=png&amp;s=30483?raw=true" alt=""></p>
</li>
<li><p>当按<code>Esc</code>或调用退出全屏方法，退出全屏。<strong>标签栏和书签栏依然是隐藏的，网页上的元素恢复成原本的尺寸</strong>。</p>
<p> 要显示书签栏和标签栏，需要刷新一下页面。</p>
</li>
</ol>
<hr>
<h2 id="全屏API："><a href="#全屏API：" class="headerlink" title="全屏API："></a>全屏API：</h2><p><strong>总共用到6个API</strong>：</p>
<ol>
<li>浏览器是否支持全屏模式：<code>document.fullscreenEnabled</code></li>
<li>使元素进入全屏模式：<code>Element.requestFullscreen()</code></li>
<li>退出全屏：<code>document.exitFullscreen()</code></li>
<li>检查当前是否有节点处于全屏状态：<code>document.fullscreenElement</code></li>
<li>进入全屏/离开全屏，触发事件：<code>document.fullscreenchange</code></li>
<li>无法进入全屏时触发: <code>document.fullscreenerror</code></li>
</ol>
<h3 id="浏览器前缀："><a href="#浏览器前缀：" class="headerlink" title="浏览器前缀："></a>浏览器前缀：</h3><p>目前并不是所有的浏览器都实现了API的无前缀版本，所以我们需要针对不同浏览器，做一下API的兼容:</p>
<p>这是我在<a href="https://codepen.io/OBKoro1/pen/wQmOzG" target="_blank" rel="noopener">demo</a>中做的浏览器兼容：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @description: 是否支持全屏+判断浏览器前缀</span></span><br><span class="line"><span class="comment"> * @param &#123;Function&#125; fn 不支持全屏的回调函数 这里设了一个默认值</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">isFullscreen(fn) &#123;</span><br><span class="line">  <span class="keyword">let</span> fullscreenEnabled;</span><br><span class="line">  <span class="comment">// 判断浏览器前缀</span></span><br><span class="line">  <span class="keyword">if</span> (<span class="built_in">document</span>.fullscreenEnabled) &#123;</span><br><span class="line">    fullscreenEnabled = <span class="built_in">document</span>.fullscreenEnabled;</span><br><span class="line">  &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="built_in">document</span>.webkitFullscreenEnabled) &#123;</span><br><span class="line">    fullscreenEnabled = <span class="built_in">document</span>.webkitFullscreenEnabled;</span><br><span class="line">    <span class="keyword">this</span>.prefixName = <span class="string">'webkit'</span>;</span><br><span class="line">  &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="built_in">document</span>.mozFullScreenEnabled) &#123;</span><br><span class="line">    fullscreenEnabled = <span class="built_in">document</span>.mozFullScreenEnabled;</span><br><span class="line">    <span class="keyword">this</span>.prefixName = <span class="string">'moz'</span>;</span><br><span class="line">  &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="built_in">document</span>.msFullscreenEnabled) &#123;</span><br><span class="line">    fullscreenEnabled = <span class="built_in">document</span>.msFullscreenEnabled;</span><br><span class="line">    <span class="keyword">this</span>.prefixName = <span class="string">'ms'</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">if</span> (!fullscreenEnabled) &#123;</span><br><span class="line">    <span class="keyword">if</span> (fn !== <span class="literal">undefined</span>) fn(); <span class="comment">// 执行不支持全屏的回调</span></span><br><span class="line">    <span class="keyword">this</span>.isFullscreenData = <span class="literal">false</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>我在实例化的时候进行一次判断浏览器是否支持全屏，然后<strong>保存浏览器前缀</strong>。</p>
<p>推荐这么做，因为如果每个API都要这样重复的判断浏览器前缀，那也太恶心了！</p>
<h3 id="1-浏览器是否支持全屏模式：document-fullscreenEnabled"><a href="#1-浏览器是否支持全屏模式：document-fullscreenEnabled" class="headerlink" title="1. 浏览器是否支持全屏模式：document.fullscreenEnabled"></a>1. 浏览器是否支持全屏模式：document.fullscreenEnabled</h3><p><code>document.fullscreenEnabled</code>属性返回一个布尔值，表示当前文档是否可以切换到全屏状态。</p>
<p>代码在上方浏览器前缀代码中给出了。</p>
<p><strong>如果没有保存浏览器前缀的话，注意做一下不同浏览器前缀的兼容！下面不再强调</strong>。</p>
<h3 id="2-使元素进入全屏模式：Element-requestFullscreen"><a href="#2-使元素进入全屏模式：Element-requestFullscreen" class="headerlink" title="2. 使元素进入全屏模式：Element.requestFullscreen()"></a>2. 使元素进入全屏模式：Element.requestFullscreen()</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @description: 将传进来的元素全屏</span></span><br><span class="line"><span class="comment"> * @param &#123;String&#125; domName 要全屏的dom名称</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">Fullscreen(domName) &#123;</span><br><span class="line">  <span class="keyword">const</span> element = <span class="built_in">document</span>.querySelector(domName); <span class="comment">// 获取dom</span></span><br><span class="line">  <span class="keyword">const</span> methodName =</span><br><span class="line">    <span class="keyword">this</span>.prefixName === <span class="string">''</span></span><br><span class="line">      ? <span class="string">'requestFullscreen'</span></span><br><span class="line">      : <span class="string">`<span class="subst">$&#123;<span class="keyword">this</span>.prefixName&#125;</span>RequestFullScreen`</span>; <span class="comment">// API前缀</span></span><br><span class="line">  element[methodName](); <span class="comment">// 调用全屏</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这就是我们<strong>实现全屏的API</strong>，是不是超简单？</p>
<p>值得注意的是：<strong>调用此API并不能保证元素一定能够进入全屏模式</strong></p>
<ol>
<li><p>MDN：例如<code>&lt;iframe&gt;</code> 元素具有 allowfullscreen 属性，可选择是否将其内容以全屏模式显示</p>
<p> 这种不被允许全屏的元素属于极少数情况，我试过可以将<code>button</code>全屏。</p>
</li>
<li><p>全屏请求必须在事件处理函数(<code>点击事件等</code>)中调用，否则将会被拒绝。</p>
<p>在<a href="https://codepen.io/OBKoro1/pen/wQmOzG" target="_blank" rel="noopener">demo</a>中有演示,初始化直接全屏，会触发进入全屏失败回调。</p>
</li>
</ol>
<h3 id="3-退出全屏：document-exitFullscreen"><a href="#3-退出全屏：document-exitFullscreen" class="headerlink" title="3. 退出全屏：document.exitFullscreen()"></a>3. 退出全屏：document.exitFullscreen()</h3><p><strong>介绍</strong>：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">exitFullscreen() &#123;</span><br><span class="line">  <span class="keyword">const</span> methodName =</span><br><span class="line">    <span class="keyword">this</span>.prefixName === <span class="string">''</span></span><br><span class="line">      ? <span class="string">'exitFullscreen'</span></span><br><span class="line">      : <span class="string">`<span class="subst">$&#123;<span class="keyword">this</span>.prefixName&#125;</span>ExitFullscreen`</span>; <span class="comment">// API 前缀</span></span><br><span class="line">  <span class="built_in">document</span>[methodName](); <span class="comment">// 调用</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>调用这个方法会让文档回退到上一个调用Element.requestFullscreen()方法进入全屏模式之前的状态。</p>
<p><strong>多层全屏</strong></p>
<p>像<a href="https://codepen.io/OBKoro1/pen/wQmOzG" target="_blank" rel="noopener">demo</a>中，先进入左边全屏，再进入红色全屏，即为：多层全屏的情况(虽然这种情况并不多)。</p>
<p><strong>当出现多层全屏的情况，需要一层层的退出到页面最初始的情况</strong>，并不是调用一次<code>document.exitFullscreen()</code>就恢复到页面最初始的样子。</p>
<h3 id="4-检查当前是否有节点处于全屏状态：document-fullscreenElement"><a href="#4-检查当前是否有节点处于全屏状态：document-fullscreenElement" class="headerlink" title="4. 检查当前是否有节点处于全屏状态：document.fullscreenElement"></a>4. 检查当前是否有节点处于全屏状态：document.fullscreenElement</h3><p>fullscreenElement属性返回正处于全屏状态的Element节点，如果当前没有节点处于全屏状态，则返回null</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @description: 检测有没有元素处于全屏状态</span></span><br><span class="line"><span class="comment"> * @return 布尔值</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">isElementFullScreen() &#123;</span><br><span class="line">  <span class="keyword">const</span> fullscreenElement =</span><br><span class="line">    <span class="built_in">document</span>.fullscreenElement ||</span><br><span class="line">    <span class="built_in">document</span>.msFullscreenElement ||</span><br><span class="line">    <span class="built_in">document</span>.mozFullScreenElement ||</span><br><span class="line">    <span class="built_in">document</span>.webkitFullscreenElement; <span class="comment">// 有前缀的f是大写，没前缀是小写</span></span><br><span class="line">  <span class="keyword">if</span> (fullscreenElement === <span class="literal">null</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span>; <span class="comment">// 当前没有元素在全屏状态</span></span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>; <span class="comment">// 有元素在全屏状态</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>事实上，还有一个属性<code>document.fullscreen</code>，返回一个布尔值，表示文档是否处于全屏模式。</p>
<p>两个方法效果是一样，但因为IE不支持这个属性，所以这里用的是<code>document.fullscreenElement</code></p>
<h3 id="5-进入全屏-离开全屏，触发事件：document-fullscreenchange"><a href="#5-进入全屏-离开全屏，触发事件：document-fullscreenchange" class="headerlink" title="5. 进入全屏/离开全屏，触发事件：document.fullscreenchange"></a>5. 进入全屏/离开全屏，触发事件：document.fullscreenchange</h3><p>当我们进入全屏和离开全屏的时候，都会触发一个<code>fullscreenchange</code>事件。</p>
<p><strong><a href="https://developer.mozilla.org/zh-CN/docs/Web/API/Fullscreen_API#%E9%80%9A%E7%9F%A5" target="_blank" rel="noopener">MDN注意</a>：此事件不会提供任何信息，表明是进入全屏或退出全屏</strong>。</p>
<p>看了好久事件返回的信息，确实找不到一个值，表明这是在进入全屏，或者离开全屏！</p>
<p>可以说相当不人性化了！但我们可以通过检查当前是否有节点处于全屏状态，判断当前是否处于全屏模式。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @description: 监听进入/离开全屏</span></span><br><span class="line"><span class="comment"> * @param &#123;Function&#125; enter 进入全屏的回调</span></span><br><span class="line"><span class="comment"> *  @param &#123;Function&#125; quit 离开全屏的回调</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">screenChange(enter,quit) &#123;</span><br><span class="line">  <span class="keyword">if</span> (!<span class="keyword">this</span>.isFullscreenData) <span class="keyword">return</span>;</span><br><span class="line">  <span class="keyword">const</span> methodName = <span class="string">`on<span class="subst">$&#123;<span class="keyword">this</span>.prefixName&#125;</span>fullscreenchange`</span>;</span><br><span class="line">  <span class="built_in">document</span>[methodName] = <span class="function"><span class="params">e</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">this</span>.isElementFullScreen()) &#123;</span><br><span class="line">      enter &amp;&amp; enter(e); <span class="comment">// 进入全屏回调</span></span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      quit &amp;&amp; quit(e); <span class="comment">// 离开全屏的回调</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>注意：多层全屏的情况</strong></p>
<ol>
<li>先进入左边全屏(进入全屏回调)，再进入红色全屏(进入全屏回调)</li>
<li>退出全屏,此时退出红色全屏，左边仍是全屏(触发进入全屏回调)</li>
<li>出现这种情况，可以在点击按钮的时候，做一些状态限制。或者根据全屏事件返回的dom信息来进行判断。</li>
</ol>
<h3 id="6-无法进入全屏时触发-document-fullscreenerror"><a href="#6-无法进入全屏时触发-document-fullscreenerror" class="headerlink" title="6. 无法进入全屏时触发: document.fullscreenerror"></a>6. 无法进入全屏时触发: document.fullscreenerror</h3><p>进入全屏并不总是成功的，可能是技术原因，也可能是用户拒绝，我们在上文进入全文的API<code>Element.requestFullscreen()</code>部分讲过了。</p>
<p>比如全屏请求不是在事件处理函数中调用,会在这里拦截到错误</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @description: 浏览器无法进入全屏时触发</span></span><br><span class="line"><span class="comment"> * @param &#123;Function&#125; enterErrorFn 回调</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">screenError(enterErrorFn) &#123;</span><br><span class="line">  <span class="keyword">const</span> methodName = <span class="string">`on<span class="subst">$&#123;<span class="keyword">this</span>.prefixName&#125;</span>fullscreenerror`</span>;</span><br><span class="line">  <span class="built_in">document</span>[methodName] = <span class="function"><span class="params">e</span> =&gt;</span> &#123;</span><br><span class="line">    enterErrorFn &amp;&amp; enterErrorFn(e)</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="Css：-全屏模式下的样式"><a href="#Css：-全屏模式下的样式" class="headerlink" title="Css： 全屏模式下的样式"></a>Css： 全屏模式下的样式</h3><p><strong>chorme 70 下的默认会为正在全屏的dom添加两个class：稍微看一下</strong></p>
<ol>
<li>默认设置黑色背景</li>
</ol>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-pseudo">:not(</span><span class="selector-pseudo">:root)</span><span class="selector-pseudo">:-webkit-full-screen</span><span class="selector-pseudo">::backdrop</span> &#123;</span><br><span class="line">  <span class="attribute">position</span>: fixed;</span><br><span class="line">  <span class="attribute">top</span>: <span class="number">0px</span>;</span><br><span class="line">  <span class="attribute">right</span>: <span class="number">0px</span>;</span><br><span class="line">  <span class="attribute">bottom</span>: <span class="number">0px</span>;</span><br><span class="line">  <span class="attribute">left</span>: <span class="number">0px</span>;</span><br><span class="line">  background: black; // 会将背景设为黑色的 如果你没为你的dom设置背景的话，全屏下会为黑色</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ol start="2">
<li>默认样式：</li>
</ol>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-pseudo">:not(</span><span class="selector-pseudo">:root)</span><span class="selector-pseudo">:-webkit-full-screen</span> &#123;</span><br><span class="line">  <span class="attribute">object-fit</span>: contain;</span><br><span class="line">  <span class="attribute">position</span>: fixed <span class="meta">!important</span>;</span><br><span class="line">  <span class="attribute">top</span>: <span class="number">0px</span> <span class="meta">!important</span>;</span><br><span class="line">  <span class="attribute">right</span>: <span class="number">0px</span> <span class="meta">!important</span>;</span><br><span class="line">  <span class="attribute">bottom</span>: <span class="number">0px</span> <span class="meta">!important</span>;</span><br><span class="line">  <span class="attribute">left</span>: <span class="number">0px</span> <span class="meta">!important</span>;</span><br><span class="line">  <span class="attribute">box-sizing</span>: border-box <span class="meta">!important</span>;</span><br><span class="line">  <span class="attribute">min-width</span>: <span class="number">0px</span> <span class="meta">!important</span>;</span><br><span class="line">  <span class="attribute">max-width</span>: none <span class="meta">!important</span>;</span><br><span class="line">  <span class="attribute">min-height</span>: <span class="number">0px</span> <span class="meta">!important</span>;</span><br><span class="line">  <span class="attribute">max-height</span>: none <span class="meta">!important</span>;</span><br><span class="line">  <span class="attribute">width</span>: <span class="number">100%</span> <span class="meta">!important</span>;</span><br><span class="line">  <span class="attribute">height</span>: <span class="number">100%</span> <span class="meta">!important</span>;</span><br><span class="line">  <span class="attribute">transform</span>: none <span class="meta">!important</span>;</span><br><span class="line">  <span class="attribute">margin</span>: <span class="number">0px</span> <span class="meta">!important</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>全屏状态的CSS：</strong></p>
<p>全屏状态下，大多数浏览器的CSS支持:full-screen伪类，只有IE11支持:fullscreen伪类。使用这个伪类，可以对全屏状态设置单独的CSS属性。</p>
<p>以下css摘自阮一峰老师的<a href="https://javascript.ruanyifeng.com/htmlapi/fullscreen.html#toc0" target="_blank" rel="noopener">Fullscreen API：全屏操作</a></p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 针对dom的全屏设置 */</span></span><br><span class="line"><span class="selector-class">.div</span><span class="selector-pseudo">:-webkit-full-screen</span> &#123;</span><br><span class="line">  <span class="attribute">background</span>: <span class="number">#fff</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/* 全屏属性 */</span></span><br><span class="line"><span class="selector-pseudo">:-webkit-full-screen</span> &#123;&#125;</span><br><span class="line"><span class="selector-pseudo">:-moz-full-screen</span> &#123;&#125;</span><br><span class="line"><span class="selector-pseudo">:-ms-fullscreen</span> &#123;&#125;</span><br><span class="line"><span class="comment">/* 全屏伪类 当前chrome:70 不支持 */</span></span><br><span class="line"><span class="selector-pseudo">:full-screen</span> &#123;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-pseudo">:fullscreen</span> &#123;</span><br><span class="line">  <span class="comment">/* IE11支持 */</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<hr>
<h2 id="结语"><a href="#结语" class="headerlink" title="结语"></a>结语</h2><p>我们可以把全屏技术应用在H5游戏、信息流网站、视频等地方，下次再有全屏需求时，记住不要慌，回头看看过本文的<a href="https://codepen.io/OBKoro1/pen/wQmOzG" target="_blank" rel="noopener">栗子</a>，把我封装的类拿出来直接用就可以啦！</p>
<h3 id="希望看完的朋友可以点个喜欢-关注，您的支持是对我最大的鼓励。"><a href="#希望看完的朋友可以点个喜欢-关注，您的支持是对我最大的鼓励。" class="headerlink" title="希望看完的朋友可以点个喜欢/关注，您的支持是对我最大的鼓励。"></a>希望看完的朋友可以点个喜欢/关注，您的支持是对我最大的鼓励。</h3><p><a href="http://obkoro1.com/" target="_blank" rel="noopener">博客</a>、<a href="http://obkoro1.com/web_accumulate/accumulate/" target="_blank" rel="noopener">前端积累文档</a>、<a href="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/1631b6f52f7e7015.jpeg?raw=true" target="_blank" rel="noopener">公众号</a>、<a href="https://github.com/OBKoro1" target="_blank" rel="noopener">GitHub</a></p>
<p> 以上2018.12.1</p>
<p> 参考资料：</p>
<p><a href="https://javascript.ruanyifeng.com/htmlapi/fullscreen.html#toc7" target="_blank" rel="noopener">Fullscreen API：全屏操作</a></p>
<p><a href="https://developer.mozilla.org/zh-CN/docs/Web/API/Fullscreen_API" target="_blank" rel="noopener">MDN</a></p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>听说，打赏我的人最后都找到了真爱。</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/pay/weixin.jpg" alt="OBKoro1 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/pay/ali.jpg" alt="OBKoro1 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Html5/" rel="tag"><i class="fa fa-tag"></i> Html5</a>
          
            <a href="/tags/WebWorker/" rel="tag"><i class="fa fa-tag"></i> WebWorker</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/11/28/前端er来学习一下webWorker吧/" rel="next" title="前端er来学习一下webWorker吧">
                <i class="fa fa-chevron-left"></i> 前端er来学习一下webWorker吧
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/12/18/轻松掌握浏览器重绘重排原理/" rel="prev" title="轻松掌握浏览器重绘重排原理">
                轻松掌握浏览器重绘重排原理 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          
            <img class="site-author-image" itemprop="image"
              src="/uploads/hand1.jpg"
              alt="OBKoro1" />
          
            <p class="site-author-name" itemprop="name">OBKoro1</p>
            <p class="site-description motion-element" itemprop="description">种一棵树最好的时间是十年前,其次是现在</p>
        </div>

        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
            
              <a href="/archives">
            
                <span class="site-state-item-count">105</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">20</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">74</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        
          <div class="feed-link motion-element">
            <a href="/atom.xml" rel="alternate">
              <i class="fa fa-rss"></i>
              RSS
            </a>
          </div>
        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/OBKoro1" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>GitHub</a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://juejin.im/user/58714f0eb123db4a2eb95372" target="_blank" title="掘金">
                  
                    <i class="fa fa-fw fa-globe"></i>掘金</a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://obkoro1.com/web_accumulate/" target="_blank" title="前端积累">
                  
                    <i class="fa fa-fw fa-globe"></i>前端积累</a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://segmentfault.com/u/obkoro1/articles" target="_blank" title="segmentfault">
                  
                    <i class="fa fa-fw fa-globe"></i>segmentfault</a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#CodePen-Demo"><span class="nav-number">1.</span> <span class="nav-text">CodePen Demo</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#全屏功能封装在一个类里面："><span class="nav-number">2.</span> <span class="nav-text">全屏功能封装在一个类里面：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#何谓全屏？"><span class="nav-number">3.</span> <span class="nav-text">何谓全屏？</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#全屏API："><span class="nav-number"></span> <span class="nav-text">全屏API：</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#浏览器前缀："><span class="nav-number">1.</span> <span class="nav-text">浏览器前缀：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-浏览器是否支持全屏模式：document-fullscreenEnabled"><span class="nav-number">2.</span> <span class="nav-text">1. 浏览器是否支持全屏模式：document.fullscreenEnabled</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-使元素进入全屏模式：Element-requestFullscreen"><span class="nav-number">3.</span> <span class="nav-text">2. 使元素进入全屏模式：Element.requestFullscreen()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-退出全屏：document-exitFullscreen"><span class="nav-number">4.</span> <span class="nav-text">3. 退出全屏：document.exitFullscreen()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-检查当前是否有节点处于全屏状态：document-fullscreenElement"><span class="nav-number">5.</span> <span class="nav-text">4. 检查当前是否有节点处于全屏状态：document.fullscreenElement</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-进入全屏-离开全屏，触发事件：document-fullscreenchange"><span class="nav-number">6.</span> <span class="nav-text">5. 进入全屏/离开全屏，触发事件：document.fullscreenchange</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-无法进入全屏时触发-document-fullscreenerror"><span class="nav-number">7.</span> <span class="nav-text">6. 无法进入全屏时触发: document.fullscreenerror</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Css：-全屏模式下的样式"><span class="nav-number">8.</span> <span class="nav-text">Css： 全屏模式下的样式</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#结语"><span class="nav-number"></span> <span class="nav-text">结语</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#希望看完的朋友可以点个喜欢-关注，您的支持是对我最大的鼓励。"><span class="nav-number">1.</span> <span class="nav-text">希望看完的朋友可以点个喜欢/关注，您的支持是对我最大的鼓励。</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy;  2017 &mdash; 
  <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">OBKoro1</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">
      156.0k
    </span>
  
</div>




<div>
<script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<span class="site-uv"><i class="fa fa-user"></i> 访问用户： <span class="busuanzi-value" id="busuanzi_value_site_uv">21621</span> 人 </span>
<span class="site-pv"><i class="fa fa-eye"></i> 访问次数： <span class="busuanzi-value" id="busuanzi_value_site_pv">68503</span> 次</span>

</div>



        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>

  
  <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.2"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.2"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.2"></script>



  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.2"></script>


  

  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.2"></script>



  


  




	





  





  








  



  





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("9ANFp60h6oVyxIPGlau3NOA4-gzGzoHsz", "tMdNuKMbFtN4QsekeNVu83TV");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  

  

  

</body>
</html>

